﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml.Linq;

namespace XEvents.Library.Targets
{
    public class Target_PairMatching : TargetData
    {
        private Target_PairMatching(SqlServer server, EventSessionTarget parent)
            : base(server, parent)
        { }

        protected override void LoadData()
        { }

        protected override void InitalizeTarget()
        {
            //throw new NotImplementedException();
        }

        internal int truncated;
        internal int orphancount;
        internal int matchedcount;
        internal int memorypressuredroppedcount;
        internal FiredEventCollection events;
 
        /*
        <PairingTarget truncated="0" orphanCount="0" matchedCount="1120" memoryPressureDroppedCount="0" />
        */
        public int Truncated
        {
            get { return truncated; }
        }

        public int OrphanCount
        {
            get { return orphancount; }
        }

        public int MatchedCount
        {
            get { return matchedcount; }
        }

        public int MemoryPressureDroppedCount
        {
            get { return memorypressuredroppedcount; }
        }

        public FiredEventCollection Events
        {
            get { return events; }
        }

        internal static TargetData GetTargetData(string data, SqlServer server, EventSessionTarget parent)
        {
            XDocument targetdata = XDocument.Parse(data);
            //XElement targetdata = XElement.Parse(data);

            return (from e in targetdata.Elements()
                    select new Target_PairMatching(server, parent)
                    {
                        truncated = (int)e.Attribute("truncated"),
                        orphancount = (int)e.Attribute("orphanCount"),
                        matchedcount = (int)e.Attribute("matchedCount"),
                        memorypressuredroppedcount = (int)e.Attribute("memoryPressureDroppedCount"),
                        events = new FiredEventCollection(
                            from fe in e.Elements()
                            select new FiredEvent
                            {
                                name = (string)fe.Attribute("name"),
                                package = (string)fe.Attribute("package"),
                                id = (string)fe.Attribute("id"),
                                version = (string)fe.Attribute("version"),
                                timestamp = (string)fe.Attribute("timestamp"),
                                payloaddata = new EventPayloadItemCollection((
                                    from pc in fe.Descendants("data")
                                    select new EventPayloadItem(server)
                                    {
                                        payloaditemtype = "Column",
                                        name = (string)pc.Attribute("name"),
                                        package = (string)pc.Attribute("package"),
                                        type_name = (string)pc.Element("type").Attribute("name"),
                                        type_package = (string)pc.Element("type").Attribute("package"),
                                        value = (string)pc.Element("value").Value,
                                        text = (string)pc.Element("text").Value
                                    }).Union<EventPayloadItem>(
                                    from pa in fe.Descendants("action")
                                    select new EventPayloadItem(server)
                                    {
                                        payloaditemtype = "Action",
                                        name = (string)pa.Attribute("name"),
                                        package = (string)pa.Attribute("package"),
                                        type_name = (string)pa.Element("type").Attribute("name"),
                                        type_package = (string)pa.Element("type").Attribute("package"),
                                        value = (string)pa.Element("value").Value,
                                        text = (string)pa.Element("text").Value
                                    }))
                            })
                    }).ElementAt(0);
        }

        public System.Data.DataTable GetEventsAsDataTable()
        {
            IEnumerable<string> q = (from t in this.Events
                                     from e in t.EventPayloadItemCollection
                                     select e.Name).Distinct();

            System.Data.DataTable dt = new System.Data.DataTable("EventInformation");

            dt.Columns.Add("Id");
            dt.Columns.Add("Name");
            dt.Columns.Add("Package");
            dt.Columns.Add("Version");
            dt.Columns.Add("TimeStamp");

            foreach (string s in q)
            {
                dt.Columns.Add(s);
            }

            foreach (FiredEvent fe in this.Events)
            {
                System.Data.DataRow dr = dt.Rows.Add();
                dr["Id"] = fe.Id;
                dr["Name"] = fe.Name;
                dr["Package"] = fe.Package;
                dr["Version"] = fe.Version;
                dr["TimeStamp"] = fe.Timestamp;
                foreach (EventPayloadItem epi in fe.EventPayloadItemCollection)
                {
                    dr[epi.Name] = epi.Value;
                }
                //dt.Rows.Add(dr);

            }
            return dt;
        }

    }
}
